package ar.com.photo_admin.taglibs

class FacebookHelperTagLib {

	static namespace = "f"

	def facebookGraphService

	def springSecurityService

	/**
	 * @attr id The id of the user whose pic will be output
	 */
	def pic = { attrs, body ->
		def id = "me"
		if (attrs.id) {
			id = attrs.id
		}
		out << """<img src="${facebookGraphService.getProfilePhotoSrc(id)}" />""";
	}

	def ifProfileAvatar = {attrs, body ->
		def username = session.user.login
		def currentUser = Account.findByLogin(username)
		if (currentUser.profile && currentUser.profile.avatar) {
			out << "avatar found"
			out << body() // Use () not {}
		}
	}

	def hasFacebookAccount = { attrs, body ->
		if (springSecurityService.isLoggedIn()) {
			def user = springSecurityService.currentUser;

			if (user.facebookUid)
				out << body()
		}
	}

	def hasNotFacebookAccount = { attrs, body ->
		if (springSecurityService.isLoggedIn()) {
			def user = springSecurityService.currentUser;

			if (!user.facebookUid)
				out << body()
		}
	}
	
	def isFacebookLoggedIn = { attrs, body ->
		if (session.facebook)
			out << body()
	}
	
	def isFacebookNotLoggedIn = { attrs, body ->
		if (!session.facebook)
			out << body()
	}
}
